var mongoose = require('mongoose');
count = 0;
//聊天记录模型
var MessageSchema = new mongoose.Schema({
  username: String,
  receive: String,
  type:{
    type: Number,
    default: 1
  },  //1:对客服讲话,2:对人讲话,3:系统广播,4:大厅全局聊天
  src: String, //保存后图片的地址
  msg: {
    type: String,
    default: ''
  },
  img: {   //base64图片
    type: String,
    default: ''
  },
  time: {
    type: Date,
    default: Date.now()
  },
  _incId:{
    type:Number,
    default: (new Date().getTime())
  }
},
{ timestamps: true }
)


MessageSchema.pre('save', function (next) {
  this._incId = (new Date()).getTime() + count++;
  next();
})


//静态方法
MessageSchema.statics = {
  fetch: function (cb) {
    return this
      .find({})
      .sort('time')
      .exec(cb)
  },
  findById: function (id, cb) {
    return this
      .findOne({_id: id})
      .exec(cb)
  }
}
module.exports = MessageSchema